package bulk_param.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

import bulk_param.dao.InactivationPointDao;
import bulk_param.modele.InactivationPointModel;
import bulk_param.modele.ReadCVS;
import bulk_param.view.Inactive_Contact_Num_View;


public class ButtonInactivationController implements ActionListener{
	
	private Inactive_Contact_Num_View inactive_Contact_Num_View;
	private InactivationPointModel inactivationPointModel;
	private InactivationPointDao inactivationPointDao;
	private ReadCVS readCVS;
	
	
	
	public ButtonInactivationController(Inactive_Contact_Num_View inactive_Contact_Num_View){
		this.inactive_Contact_Num_View=inactive_Contact_Num_View;
		inactivationPointModel = new InactivationPointModel();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getSource().equals(inactive_Contact_Num_View.btInactiver)){
			System.out.println(" Access inactivation de Point ZABPQ ");
			//Remise des contours champs à gris	
			inactive_Contact_Num_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.lightGray));
			inactive_Contact_Num_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.lightGray));
			inactive_Contact_Num_View.cbPays.setBorder(BorderFactory.createLineBorder(Color.lightGray));
			
			// TYPE DE TRAFIC NATIONAL
			if(inactive_Contact_Num_View.national.isSelected()==true){
				if(!inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()
					&& inactive_Contact_Num_View.dateChooser.getDate() != null){
					if(!inactive_Contact_Num_View.txtZABPQ.getText().matches(inactivationPointModel.getZABPQ_REGEX())){
						inactive_Contact_Num_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
						JOptionPane.showMessageDialog(null,inactivationPointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);										
					}else{
						//Verification si le point existe ou pas
						inactivationPointDao = new InactivationPointDao();
						boolean isExist = inactivationPointDao.isPointExist("33",inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
						
						if(isExist == false){
							String msg = "Le point : 33" + inactive_Contact_Num_View.txtZABPQ.getText().substring(1) + "\nn'éxiste pas";
			                JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
						}
						else{	
							try {
								if(inactivationPointDao.isDateBefore("33",inactive_Contact_Num_View.txtZABPQ.getText().substring(1),inactive_Contact_Num_View.dateChooser.getDate())==false){
									Timestamp inBase= inactivationPointDao.dateInBase("33", inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
									String pointZABPQ= "33"+inactive_Contact_Num_View.txtZABPQ.getText().substring(1);
									Date date= inactive_Contact_Num_View.dateChooser.getDate();
									inactivationPointDao.inactivePointZABPQ(pointZABPQ,date,inBase);
									String succesMsg= "Le point : " +33+ inactive_Contact_Num_View.txtZABPQ.getText().substring(1) + "\na été inactivé avec succès";
									JOptionPane.showMessageDialog(null, succesMsg, "Message", JOptionPane.INFORMATION_MESSAGE);	
								}else{
									Timestamp date= inactivationPointDao.dateInBase("33", inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
									String dateMsg="Vous devez choisir une date \négale ou postérieure à la date : "+new java.sql.Date(date.getTime());
									JOptionPane.showMessageDialog(null, dateMsg, "Attention", JOptionPane.WARNING_MESSAGE);	
								}					
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						inactivationPointDao.closeConnection();
					}					
				}else if(inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()
						|| inactive_Contact_Num_View.dateChooser.getDate() == null){
					if (inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,inactivationPointModel.getINACTIVATION_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					JOptionPane.showMessageDialog(null,inactivationPointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
						inactive_Contact_Num_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
						System.out.println("INACTIVATION : Point ZABPQ vide");
					}else if(inactive_Contact_Num_View.dateChooser.getDate() == null){
					JOptionPane.showMessageDialog(null,inactivationPointModel.getINACTIVATION_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					JOptionPane.showMessageDialog(null,inactivationPointModel.getDATE_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					inactive_Contact_Num_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : La date n'est pas renseigné");
					}
				}
				
			// TYPE DE TRAFIC INTERNATIONAL
			}else{
				if(!inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()
					&& inactive_Contact_Num_View.cbPays.getSelectedItem() != null
					&& inactive_Contact_Num_View.dateChooser.getDate() != null){
					//Verification si le point existe ou pas
					inactivationPointDao = new InactivationPointDao();
					readCVS= new ReadCVS();
					String mssg="Le préfixe et/ou le pays "+inactive_Contact_Num_View.cbPays.getSelectedItem().toString()+" \nn'est pas parametré dans le fichier : \n'prefixes_internationaux.txt'";			
					try{
						String indicatif=readCVS.indicatifDictionary(inactive_Contact_Num_View.cbPays.getSelectedItem().toString());
						String indicatifPays = indicatif;
						int isIndicatifExist = Integer.valueOf(indicatifPays.trim());															
						boolean isExist = inactivationPointDao.isPointExist(indicatif,inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
						if(isExist == false){
							String msg = "Le point : " +indicatif+ inactive_Contact_Num_View.txtZABPQ.getText().substring(1) + "\néxiste pas";
			                JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
						}
						else{
							try{
							if(inactivationPointDao.isDateBefore(indicatif,inactive_Contact_Num_View.txtZABPQ.getText().substring(1),inactive_Contact_Num_View.dateChooser.getDate())==false){
								String pointZABPQ= indicatif+inactive_Contact_Num_View.txtZABPQ.getText().substring(1);
								Date date= (Date) inactive_Contact_Num_View.dateChooser.getDate();
								Timestamp inBase= inactivationPointDao.dateInBase(indicatif, inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
								inactivationPointDao.inactivePointZABPQ(pointZABPQ,date,inBase);
								String succesMsg= "Le point : " +indicatif+ inactive_Contact_Num_View.txtZABPQ.getText().substring(1) + "\na été inactivé avec succès";
								JOptionPane.showMessageDialog(null, succesMsg, "Message", JOptionPane.INFORMATION_MESSAGE);		
							
							}else{
								Timestamp date= inactivationPointDao.dateInBase(indicatif, inactive_Contact_Num_View.txtZABPQ.getText().substring(1));
								String dateMsg="Vous devez choisir une date \négale ou postérieure à la date : "+new java.sql.Date(date.getTime());
								JOptionPane.showMessageDialog(null, dateMsg, "Attention", JOptionPane.WARNING_MESSAGE);	
							}					
							} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							}
						}
						inactivationPointDao.closeConnection();
					}catch (NumberFormatException e) {
							// TODO: handle exception
							JOptionPane.showMessageDialog(null, mssg, "Attention", JOptionPane.WARNING_MESSAGE);							
					}		
				}else{
					if(inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()
						&& inactive_Contact_Num_View.cbPays.getSelectedItem() == null
						&& inactive_Contact_Num_View.dateChooser.getDate() == null){
					JOptionPane.showMessageDialog(null,inactivationPointModel.getINACTIVATION_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					inactive_Contact_Num_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
					inactive_Contact_Num_View.cbPays.setBorder(BorderFactory.createLineBorder(Color.red));
					inactive_Contact_Num_View.dateEditor.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : Tout les champs sont obligatoires");
					}else if (inactive_Contact_Num_View.cbPays.getSelectedItem() == null) {
					JOptionPane.showMessageDialog(null,inactivationPointModel.getPAYS_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					inactive_Contact_Num_View.cbPays.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : pas de selection de Pays");
					}else if (inactive_Contact_Num_View.txtZABPQ.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,inactivationPointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					inactive_Contact_Num_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : POINT ZABPQ vide");
					}else if(inactive_Contact_Num_View.dateChooser.getDate() == null){
					JOptionPane.showMessageDialog(null,inactivationPointModel.getDATE_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					inactive_Contact_Num_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : La date n'est pas renseigné");
				}
				}
			}
		}		
	}
}


